package com.mlnx.web.ui.controller;

import com.mlnx.common.entity.Response;
import com.mlnx.ui.entity.Patient;
import com.mlnx.ui.pojo.form.PatientAlarmForm;
import com.mlnx.ui.pojo.form.PatientForm;
import com.mlnx.ui.pojo.form.XKParamForm;
import com.mlnx.ui.service.PatientService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.shan.spring.base.BaseController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

@Api(value = "UI患者接口", tags = {"UI患者接口"})
@RestController
@RequestMapping("ui/patient")
public class PatientController extends BaseController {

    @Autowired
    private PatientService patientService;

    @ApiOperation(value = "按群组获取所有患者以及在线状态")
    @GetMapping(value = "")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userId", value = "用户ID", required = true)
    })
    public Response getPatients(@RequestParam("userId") Integer userId) {
        return result(patientService.getAllPatients(userId, false));
    }

    @ApiOperation(value = "按群组获取所有患者以及在线状态")
    @GetMapping(value = "/taizhou")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userId", value = "用户ID", required = true)
    })
    public Response getTaizhouPatients(@RequestParam("userId") Integer userId) {
        return result(patientService.getAllPatients(userId, true));
    }

    @ApiOperation(value = "获取历史血压界面患者列表")
    @GetMapping(value = "/taizhou2")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userId", value = "用户ID", required = true)
    })
    public Response getTaizhouPatients2(@RequestParam("userId") Integer userId) {
        return result(patientService.getTaizhouPatients(userId));
    }

    @ApiOperation(value = "添加患者")
    @PostMapping(value = "")
    public Response addPatient(@Valid PatientForm patientForm) {
        Integer patientId = patientService.addPatient(patientForm);

        return result(patientId);
    }

    @ApiOperation(value = "转移患者至另一个群组")
    @PutMapping(value = "")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "groupId", value = "群组ID", required = true),
            @ApiImplicitParam(name = "patientIds", value = "用户ID集合", required = true)
    })
    public Response changePatientGroup(@RequestParam("groupId") Integer groupId, @RequestParam("patientIds") Integer[] patientIds) {
        patientService.movePatientsToGroup(groupId, patientIds);

        return result();
    }

    @ApiOperation(value = "批量删除患者")
    @DeleteMapping(value = "")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "patientIds", value = "用户ID集合", required = true)
    })
    public Response deletePatients(@RequestParam("patientIds") Integer[] patientIds) {
        patientService.deletePatients(patientIds);

        return result();
    }

    @ApiOperation(value = "通过ID修改患者备注信息")
    @PutMapping(value = "/{patientId}")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "remark", value = "患者备注", required = true)
    })
    public Response updatePatientRemark(@PathVariable("patientId") Integer patientId, @RequestParam("remark") String remark) {
        Patient patient = new Patient();
        patient.setId(patientId);
        patient.setRemark(remark);

        patientService.updateById(patient);

        return result();
    }

    @ApiOperation(value = "心康患者同步")
    @PostMapping(value = "/xk")
    public Response syncXKPatients(@Valid XKParamForm xkParamForm) {
        patientService.synXKPatients(xkParamForm);
        return result();
    }

    @ApiOperation(value = "设置报警上下限")
    @PostMapping(value = "/alarm")
    public Response setPatientAlarm(@Valid PatientAlarmForm patientAlarmForm) {
        patientService.setPatientAlarm(patientAlarmForm);

        return result();
    }

    @ApiOperation(value = "获取报警上下限")
    @PostMapping(value = "/alarm/{patientId}")
    public Response getPatientAlarm(@PathVariable Integer patientId) {
        return result(patientService.getPatientAlarm(patientId));
    }


    @ApiOperation(value = "根据手机号获取病人id和心电设备编号")
    @GetMapping(value = "/device/info")
    public Response getPatientDeviceInfo(@RequestParam String phone) {
        return result(patientService.getPatientDeviceInfo(phone));
    }
}
